package com.zhangzhan.base.handler.exception;

import com.zhangzhan.base.common_model.result.ResponseResult;
import com.zhangzhan.base.enums.AppHttpCodeEnum;
import com.zhangzhan.base.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 张一恒
 * @version 1.0
 * @description: 全局异常处理器
 * @date 2024/2/3 23:47
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * @description 系统异常处理方法
     * @param e
     * @return com.zhangzhan.base.common_model.result.ResponseResult
     * @author zhangyiheng
     * @date 2024/2/3 23:49
     */
    @ExceptionHandler(SystemException.class)
    public ResponseResult systemException(SystemException e){
        //打印异常信息
        log.error("系统出现了异常！ {}",e);
        //从异常对象中获取提示信息封装返回
        return ResponseResult.errorResult(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception e){
        //打印异常信息
        log.error("未知后端异常！ {}",e);
        //从异常对象中获取提示信息封装返回
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),e.getMessage());
    }

}
